package mobi.springtour;

import java.io.IOException;
import java.util.List;

import mobi.springtour.domain.Ticket;
import mobi.springtour.util.TicketListAdapter;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.thoughtworks.xstream.XStream;

public class TicketActivity extends ListActivity {
	private TicketListAdapter listAdapter;

	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		// 显示布局
		try {    
			setContentView(R.layout.tickets);
			// 获得从上个页面传过来的数据
			Bundle bundle = this.getIntent().getExtras();
			String str = bundle.getString("USERNAME");
			setTitle(str + " 的门票");
			Button exit = (Button) findViewById(R.id.exit);
			exit.setOnClickListener(new View.OnClickListener() {
				public void onClick(View view) {
					AlertDialog.Builder builder = new AlertDialog.Builder(
							TicketActivity.this);
					builder.setMessage("确定要退出吗?")
							.setCancelable(false)
							.setPositiveButton("是",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog, int id) {
											TicketActivity.this.finish();
										}
									})
							.setNegativeButton("否",
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog, int id) {
											dialog.cancel();
										}
									});
					AlertDialog alert = builder.create();
					alert.show();
				}
			});
			this.setListAdapter(getAdapter());
		} catch (Exception e) {
			Log.v("TicketActivity", e.getMessage(), e);
		}
	}

	private TicketListAdapter getAdapter() {
		if (this.listAdapter == null) {
			listAdapter = new TicketListAdapter(this, getList());
		}
		return listAdapter;
	}

	@SuppressWarnings("unchecked")
	private List<Ticket> getList() {
		try {
			XStream xStream = new XStream();
			xStream.alias("item", Ticket.class);
			return (List<Ticket>) xStream.fromXML(this.getAssets().open(
					"list.xml"));

		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}
}
